﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Forms;

namespace Wicresoft.Shell.Package.AppExp
{
    public class ExtensionPropertyItemObject : BasePropertyItemObject
    {
        private string iextensiontype;
        private string ikeyvalue;

        public ExtensionPropertyItemObject(TreeNode tn)
        {
            _tn = tn;

            switch (tn.ImageIndex)
            {
                case (int)MyControl.AppExpImageList.TempTxt:
                    iextensiontype = "txt";
                    break;
                case (int)MyControl.AppExpImageList.TempVm:
                    iextensiontype = "vm";
                    break;
                case (int)MyControl.AppExpImageList.TempXslt:
                    iextensiontype = "xsl";
                    break;
                case (int)MyControl.AppExpImageList.TempHtml:
                    iextensiontype = "html";
                    break;
                default:
                    iextensiontype = "txt";
                    break;
            }
            ikeyvalue = _tn.Text;
        }


        protected override void SetProperties()
        {
            base.SetProperties();
            if (_tn != null)
            {
                if (_tn.Tag != null)
                {
                    string t_tag = _tn.Tag.ToString();
                    if (t_tag.StartsWith("APIDX"))
                    {
                        string[] ids = t_tag.Substring(6).Split(';');
                        try
                        {
                            switch (iextensiontype)
                            {
                                case "txt":
                                    _tn.ImageIndex = (int)MyControl.AppExpImageList.TempTxt;
                                    _tn.SelectedImageIndex = (int)MyControl.AppExpImageList.TempTxtSelected;
                                    break;
                                case "vm":
                                    _tn.ImageIndex = (int)MyControl.AppExpImageList.TempVm;
                                    _tn.SelectedImageIndex = (int)MyControl.AppExpImageList.TempVmSelected;
                                    break;
                                case "xsl":
                                    _tn.ImageIndex = (int)MyControl.AppExpImageList.TempXslt;
                                    _tn.SelectedImageIndex = (int)MyControl.AppExpImageList.TempXsltSelected;
                                    break;
                                case "html":
                                    _tn.ImageIndex = (int)MyControl.AppExpImageList.TempHtml;
                                    _tn.SelectedImageIndex = (int)MyControl.AppExpImageList.TempHtmlSelected;
                                    break;
                                default:
                                    MessageBox.Show(@"You can only fill txt\vm\xsl\html!");
                                    return;
                                    break;
                            }
                            Workspace.GetDatabaseLogic().UpdateApplicationExtensionProfTypeAndName(Convert.ToInt16(ids[1]), iextensiontype, ikeyvalue);
                            _tn.Text = ikeyvalue;

                        }
                        catch
                        {
                            MessageBox.Show("Operation Failed!");
                        }
                    }
                    else
                    {
                        string[] ids = t_tag.Substring(6).Split(';');
                        try
                        {
                            switch (iextensiontype)
                            {
                                case "txt":
                                    _tn.ImageIndex = (int)MyControl.AppExpImageList.TempTxt;
                                    _tn.SelectedImageIndex = (int)MyControl.AppExpImageList.TempTxtSelected;
                                    break;
                                case "vm":
                                    _tn.ImageIndex = (int)MyControl.AppExpImageList.TempVm;
                                    _tn.SelectedImageIndex = (int)MyControl.AppExpImageList.TempVmSelected;
                                    break;
                                case "xsl":
                                    _tn.ImageIndex = (int)MyControl.AppExpImageList.TempXslt;
                                    _tn.SelectedImageIndex = (int)MyControl.AppExpImageList.TempXsltSelected;
                                    break;
                                default:
                                    MessageBox.Show(@"You can only fill txt\vm\xsl!");
                                    return;
                                    break;
                            }
                            Workspace.GetDatabaseLogic().UpdateFunctionExtensionProfTypeAndName(Convert.ToInt16(ids[1]), iextensiontype, ikeyvalue);
                            _tn.Text = ikeyvalue;

                        }
                        catch
                        {
                            MessageBox.Show("Operation Failed!");
                        }
                    }
                }
            }
        }

        
        [Category("General")]
        [Description("Extension Type property")]
        public string ExtensionType
        {
            get 
            {
                return iextensiontype;
            }
            set 
            {
                iextensiontype = value;
                SetProperties();
            }
        }

        [Category("General")]
        [Description("Key property")]
        public string Key
        {
            get
            {
                return ikeyvalue;
            }
            set
            {
                ikeyvalue = value;
                SetProperties();
            }
        }

    }
}
